/********************************
 * Robinson Matrix Algorithm
 *
 * Copyright (C) 2009 CNRS
 * Author : Florent AVELLANEDA, Eugen CEPOI
 * 
 *
 * All rights reserved.
 *
 *   This file is part of Robinson Matrix Algorithm.
 *
 *   Robinson Matrix Algorithm is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robinson Matrix Algorithm is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Robinson Matrix Algorithm.  If not, see <http://www.gnu.org/licenses/>.
 *
 ***********************************************************************************/

#include "AlgoGraph.h"

namespace AlgoGraph
{

    void reverseGraph( std::vector<std::set<unsigned int> > &G, std::vector<std::set<unsigned int> > &result )
    {
        result.resize(G.size());
        for ( unsigned int i = 0; i < G.size(); ++i )
        {	
            for ( std::set<unsigned int>::iterator it = G[i].begin(); it != G[i].end(); ++it )
            {
                result[(*it)].insert(i);
            }
        }
    }


    bool bipartiGraphClusters( std::vector<std::set<unsigned int> > &graph, std::vector<std::set<unsigned int> > &clusters, std::vector<unsigned int> &cells )
    {
        if ( graph.size() == 0 )
            return true;
        std::vector<int> visited;
        visited.resize(graph.size());
        cells.resize(graph.size());

        for ( unsigned int i = 0; i < visited.size(); ++i )
            visited[i] = -1;
        visited[0] = 0;
        clusters.resize(2);
        clusters[0].insert(0);
        cells[0] = 0;

        unsigned int begin = 0; 
parcours:

        if ( !bipartiGraphCluster( graph, clusters, cells, begin, visited ) )
        {
            clusters.clear();
            cells.clear();
            return false;
        }
        for ( unsigned int i = 0; i < visited.size(); ++i )
            if ( visited[i] < 0 )
            {
                begin = i;
                visited[i] = 0;
                cells[i] = clusters.size();
                clusters.resize(clusters.size()+2);
                clusters[clusters.size()-2].insert(begin);

                goto parcours;
            }

        return true;
    }

    bool bipartiGraphCluster( std::vector<std::set<unsigned int> > &graph, std::vector<std::set<unsigned int> > &clusters, std::vector<unsigned int> &cells, unsigned int begin, std::vector<int> &visited )
    {
        for ( std::set<unsigned int>::iterator it = graph[begin].begin(); it != graph[begin].end(); ++it )
            if ( visited[(*it)] == visited[begin] )
                return false;
            else if ( visited[(*it)] == -1 )
            {
                visited[(*it)] = (visited[begin]+1)%2;
                if ( visited[(*it)] == 1 )
                {
                    cells[(*it)] = clusters.size()-1;
                    clusters.back().insert((*it));
                }
                else
                {
                    cells[(*it)] = clusters.size()-2;
                    clusters[clusters.size()-2].insert((*it));
                }
                if( !bipartiGraphCluster( graph, clusters, cells, (*it), visited ) )
                    return false;
            }

        return true;
    }
}
